﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SalePoints.Utility;

namespace SalePoints.Views
{
	public partial class ZoomWindow :ChildWindow
	{
		public ZoomWindow()
		{
			InitializeComponent();
			this.Closed += new EventHandler(ZoomWindow_Closed);
			VerticalGap = 84;
			HorizontalGap = 40;
		}

		void ZoomWindow_Closed(object sender, EventArgs e)
		{
			Reset();
			IsZooming = false;
		}
		private Panel RawWrapper;
		private FrameworkElement ZoomTarget;
		private Size RawSize;
		private Size RawZoomerSize;
		public bool IsZooming;
		public double VerticalGap { get; set; }
		public double HorizontalGap { get; set; }

		public ZoomWindow Zoom(Panel rawWrapper, FrameworkElement obj,Size finalSize)
		{
			if (IsZooming) return this;
			//size
			ResolveZoomerSize();

			RawWrapper = rawWrapper; 
			ZoomTarget = obj;
			RawWrapper.Children.Remove(obj);
			//size
			RawSize = new Size() {Height=obj.Height,Width=obj.Width };
			if (finalSize == Size.Empty) {
				finalSize = new Size() {Width=this.Width*95/100,Height=this.Height*70/100 };
			};
			
			chartWrapper.Children.Clear();
			chartWrapper.Children.Add(ZoomTarget);
			this.Show();

			ZoomTarget.Width = finalSize.Width;
			ZoomTarget.Height = finalSize.Height;

			IsZooming = true;

			return this;
		}
		private void OKButton_Click(object sender, RoutedEventArgs e)
		{
			this.DialogResult = true;
		}
		private void Reset(){
			if (null == ZoomTarget) return;
			chartWrapper.Children.Remove(ZoomTarget);
			ZoomTarget.Height = RawSize.Height;
			ZoomTarget.Width = RawSize.Width;
			RawWrapper.Children.Add(ZoomTarget);

			//reset zoom window size
			this.Height = RawZoomerSize.Height;
			this.Width = RawZoomerSize.Width;

			RawWrapper = null;
			ZoomTarget = null;
		}

		private Size ResolveZoomerSize() {
			RawZoomerSize = new Size { Width=this.Width,Height=this.Height};

			if (this.Width.Equals(double.NaN))
			{
				if (App.Current.IsRunningOutOfBrowser) {
					this.Width = 1024;
				}
				else
				{
					this.Width = BrowserHelper.ClientWidth < MinWidth ? MinWidth : BrowserHelper.ClientWidth;
					if (this.Width.Equals(BrowserHelper.ClientWidth))
					{
						this.Width = this.Width - HorizontalGap;
					}
				};

			};
			//height
			if (this.Height.Equals(double.NaN))
			{
				if (App.Current.IsRunningOutOfBrowser){
					this.Height=768;
				}else{
					this.Height = BrowserHelper.ClientHeight < MinHeight ? MinHeight : BrowserHelper.ClientHeight;
					if (this.Height.Equals(BrowserHelper.ClientHeight)) {
						this.Height = this.Height - VerticalGap;
					};
				}
			};

			return new Size { Height=this.Height,Width=this.Width};
		}
	}
}

